package com.itheima.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
   private StudentMapper studentMapper;


    @Override
    public PageResult<Student> list(StudentQueryParam studentQueryParam) {
        //分页查询
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());

        //查询
        List<Student> studentList = studentMapper.list(studentQueryParam);
        Page<Student> page = (Page<Student>) studentList;
        return new PageResult<Student>(page.getTotal(), page.getResult());
    }

    @Override
    public void deleteById(List<Integer> ids) {
        log.info("delete student ids: {}", ids);
        studentMapper.deleteById(ids);
    }

    //添加数据  添加数据时注意设置createTime和updateTime
    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        log.info("add student: {}", student);
        studentMapper.add(student);
    }

    @Override
    public Student get(Integer id) {
        log.info("get student id: {}", id);
        Student student = studentMapper.get(id);
        return student;
    }

    @Override //修改数据时注意设置updateTime
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        log.info("update student: {}", student);
        studentMapper.update(student);
    }

    @Override
    public void violation(Integer id, Integer score) {
        log.info("violation student id: {}, score: {}");
        studentMapper.violation(id,score);
    }


}
